/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package databook.controller;

import javax.swing.JCheckBox;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

/**
 *
 * @author zach
 */
public class PropertyCheckBox extends JCheckBox implements PropertyListener, ActionListener {
    private String key;
    
    public PropertyCheckBox(String text,String key) {
        this(text,key,false);
    }
    
    public PropertyCheckBox(String text, String key, boolean def) {
        super();
        setText(text);
        init(key,def);
    }
    
    public PropertyCheckBox(String key) {
        this(key,false);
    }
    
    public PropertyCheckBox(String key, boolean def) {
        init(key,def);
    }
    
    public void init(String key,boolean def) {
        this.key = key;
        setSelected(Props.getBoolean(key,def));
        Props.addListener(key,this);
        addActionListener(this);
    }
        
    public void propertyChanged(PropertyEvent e) {
        boolean val = ((Boolean)e.value).booleanValue();
        if (this.isSelected() != val) {
            this.setSelected(val);
        }
    }
    
    public void actionPerformed(ActionEvent evt) {
        if (Props.getBoolean(key) != isSelected()) 
            Props.set(key,isSelected());
    }
}
